package com.whmnx.chitchat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.friendship.TIMFriend;
import com.tencent.imsdk.friendship.TIMFriendRequest;
import com.tencent.imsdk.friendship.TIMFriendResult;
import com.tencent.imsdk.friendship.TIMFriendStatus;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.utils.TUIKitLog;
import com.tencent.qcloud.tim.uikit.utils.ToastUtil;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.my.MyHeiActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.internal.Utils;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2020/7/22 0022 10:20
 * version: 1.0
 *
 * @author Administrator
 */
public class FriendInfoActivity extends BaseActivity {
    @BindView(R.id.iv_user)
    CircleImageView ivUser;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_name1)
    TextView tvName1;
    @BindView(R.id.tv_niceName)
    TextView tvNiceName;
    @BindView(R.id.tv_com)
    TextView tvCom;
    @BindView(R.id.tv_department)
    TextView tvDepartment;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.sb_add)
    Button sbAdd;

    private String friendMobile;
    private UserBean loginBean;
    private int type2,state;
    private String url,id;
    private UserBean loginBean2;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.friend_info_activity);
    }

    @Override
    protected void loadData() {
          type2 = getIntent().getIntExtra("type",0);
          loginBean = (UserBean) getIntent().getSerializableExtra("result");
           state =  getIntent().getIntExtra("state",0);
          String  userId = getIntent().getStringExtra("userId");
         if (type2 == 1){
            sbAdd.setText("删除好友");
            Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            HttpClient.get(FriendInfoActivity.this, Constants.GETUSERINFO, params, new CallBack<UserBean>() {
                @Override
                public void onSuccess(UserBean loginBean) {
                    setTitle(loginBean.getUserNick());
                    friendMobile = loginBean.getPhone();
                    id = loginBean.getUserId();
                    ImageUtil.setImage(ivUser, loginBean.getHeadImg());
                    tvName.setText(loginBean.getUserName());
                    tvName1.setText(loginBean.getUserName());
                    tvNiceName.setText(loginBean.getUserNick());
                    tvCom.setText(loginBean.OrganizeName);
                    tvDepartment.setText(loginBean.DepartmentName);
                    tvPhone.setText(loginBean.getPhone());
                }
            });
        }else if (type2 == 2){
            // sbAdd.setVisibility(View.GONE);
             setIbRight(View.VISIBLE);
             Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            HttpClient.get(FriendInfoActivity.this, Constants.GETUSERINFO, params, new CallBack<UserBean>() {
                @Override
                public void onSuccess(UserBean loginBean) {
                    loginBean2 = loginBean;
                    setTitle(loginBean.getUserNick());
                    friendMobile = loginBean.getPhone();
                    id = loginBean.getUserId();
                    ImageUtil.setImage(ivUser, loginBean.getHeadImg());
                    tvName.setText(loginBean.getUserName());
                    tvName1.setText(loginBean.getUserName());
                    tvNiceName.setText(loginBean.getUserNick());
                    tvCom.setText(loginBean.OrganizeName);
                    tvDepartment.setText(loginBean.DepartmentName);
                    tvPhone.setText(loginBean.getPhone());
                    sbAdd.setText("发消息");
                }
            });
            //向右箭头
            ib_right.setOnClickListener(v -> {
                startActivity(new Intent(this,ChatPerSettingActivity.class).putExtra("ToUserID",id));
            });
        }else {
            setTitle(loginBean.getUserNick());
            friendMobile = loginBean.getPhone();
            ImageUtil.setImage(ivUser, loginBean.getHeadImg());
            tvName.setText(loginBean.getUserName());
            tvName1.setText(loginBean.getUserName());
            tvNiceName.setText(loginBean.getUserNick());
            tvCom.setText(loginBean.OrganizeName);
            tvDepartment.setText(loginBean.DepartmentName);
            tvPhone.setText(loginBean.getPhone());
            id = loginBean.getUserId();
            if (state == 2){
                sbAdd.setText("发消息");
            }else if (state == 3){
                sbAdd.setText("已拉黑");
                sbAdd.setClickable(false);
            }
        }
    }




    @OnClick({R.id.bt_msg, R.id.sb_add,R.id.ll_user})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_user:
             //   ChatInfo  chatInfo = new ChatInfo();
             //   chatInfo.setChatName(loginBean.getUserNick());
               // chatInfo.setId(loginBean.getUserId());
                startActivity(new Intent(this, ChatSettingActivity.class).putExtra("id", id));
                break;
            case R.id.sb_add:
                /*TIMFriendRequest timFriendRequest = new TIMFriendRequest(friendMobile);
                timFriendRequest.setAddWording(friendMobile);
                timFriendRequest.setAddSource("android");
                TIMFriendshipManager.getInstance().addFriend(timFriendRequest, new TIMValueCallBack<TIMFriendResult>() {
                    @Override
                    public void onError(int i, String s) {
                        //    DemoLog.e(TAG, "addFriend err code = " + i + ", desc = " + s);
                        // ToastUtil.toastShortMessage("Error code = " + i + ", desc = " + s);
                        UItils.showToastSafe("用户不存在");
                    }

                    @Override
                    public void onSuccess(TIMFriendResult timFriendResult) {
                        //  DemoLog.i(TAG, "addFriend success result = " + timFriendResult.toString());
                        switch (timFriendResult.getResultCode()) {
                            case TIMFriendStatus.TIM_FRIEND_STATUS_SUCC:
                                HashMap<String, Object> hashMap = new HashMap<>();
                                // 修改好友备注
                                hashMap.put("Tag_SNS_Custom_Type", "1");
                                TIMFriendshipManager.getInstance().modifyFriend(friendMobile, hashMap, new TIMCallBack() {
                                    @Override
                                    public void onError(int i, String s) {

                                    }

                                    @Override
                                    public void onSuccess() {
                                        UItils.showToastSafe("修改好友成功");
                                        finish();
                                    }
                                });
                             //   UItils.showToastSafe("添加好友成功");

                                break;
                            case TIMFriendStatus.TIM_FRIEND_PARAM_INVALID:
                                if (TextUtils.equals(timFriendResult.getResultInfo(), "Err_SNS_FriendAdd_Friend_Exist")) {
                                    UItils.showToastSafe("对方已是您的好友");
                                    break;
                                }
                            case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_SELF_FRIEND_FULL:
                                UItils.showToastSafe("您的好友数已达系统上限");
                                break;
                            case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_THEIR_FRIEND_FULL:
                                UItils.showToastSafe("对方的好友数已达系统上限");
                                break;
                            case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_IN_SELF_BLACK_LIST:
                                UItils.showToastSafe("被加好友在自己的黑名单中");
                                break;
                            case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_FRIEND_SIDE_FORBID_ADD:
                                UItils.showToastSafe("对方已禁止加好友");
                                break;
                            case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_IN_OTHER_SIDE_BLACK_LIST:
                                UItils.showToastSafe("您已被被对方设置为黑名单");
                                break;
                            case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_PENDING:
                                UItils.showToastSafe("等待好友审核同意");
                                break;
                            default:
                                UItils.showToastSafe(timFriendResult.getResultCode() + " " + timFriendResult.getResultInfo());
                                break;
                        }
                        finish();
                    }
                });*/

                if (type2 == 1){
                     url = Constants.GETDELETEFRIEND;
                }else if (type2 == 2){  //发消息
                    ChatInfo chatInfo = new ChatInfo();
                    chatInfo.setType(TIMConversationType.C2C);
                    chatInfo.setId(loginBean2.getUserId());
                    chatInfo.setChatName(loginBean2.getUserNick());
                    Intent intent = new Intent(FriendInfoActivity.this, ChatActivity.class);
                    intent.putExtra(Constants.CHAT_INFO, chatInfo);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    return;

                }else {
                    url = Constants.GETADDFRIENDSTEMPORARY;
                }

                Map<String, Object> params = new HashMap<>();
                params.put("Mobile", SPUtil.getString(Constants.PHONE));
                params.put("FriendMobile", friendMobile);
                HttpClient.get(FriendInfoActivity.this, url, params, new CallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if (type2 == 1){
                            UItils.showToastSafe("删除好友成功");
                            MessageEvent messageEvent = new MessageEvent();
                            messageEvent.setMessageKey(EventConstants.FRIENDS);
                            EventBus.getDefault().post(messageEvent);
                        }else {
                            UItils.showToastSafe("添加好友成功");
                        }
                        finish();
                    }
                });
                break;
            default:
        }
    }
}
